package com.xiaofengstu.mcams.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaofengstu.mcams.constant.SysConstants;
import com.xiaofengstu.mcams.util.ThreadLocalUtils;
import com.xiaofengstu.mcams.web.entity.TDept;
import com.xiaofengstu.mcams.web.entity.TUser;
import com.xiaofengstu.mcams.web.mapper.TDeptMapper;
import com.xiaofengstu.mcams.web.service.TDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaofengstu.mcams.web.service.TUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fengzeng
 * @since 2022-05-07
 */
@Service
@RequiredArgsConstructor
public class TDeptServiceImpl extends ServiceImpl<TDeptMapper, TDept> implements TDeptService {

  private final TDeptMapper deptMapper;
  private final TUserService userService;
  @Override
  public List<TDept> listByRole() {
    Integer userId = ThreadLocalUtils.get();
    TUser curUser = userService.getById(userId);
    Integer roleId = curUser.getRoleId();

    List<TDept> ret = new ArrayList<>();

    if (roleId.equals(SysConstants.SYS_ADMIN_ID)
        || roleId.equals(SysConstants.SYS_CAMPUS_ADMIN_ID)) {
      ret = deptMapper.selectList(new QueryWrapper<>());
    } else {
      ret.add(deptMapper.selectById(curUser.getDeptId()));
    }
    return ret;
  }
}
